package com.leetroll.app.controller;

import com.leetroll.api.service.OrderService;
import com.leetroll.api.service.impl.ItemServiceImpl;
import com.leetroll.app.utils.AppJwtTokenUtil;
import com.leetroll.common.bean.ApiResponse;
import com.leetroll.common.entity.Order;
import com.leetroll.common.enums.EOrderStatus;
import com.leetroll.common.vo.ItemVO;
import com.leetroll.common.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/order")
@Api(value = "订单相关接口", tags = {"订单相关接口"})
@Slf4j
public class OrderController {
    private final OrderService orderService;
    private final ItemServiceImpl itemService;
    private final AppJwtTokenUtil jwtTokenUtil;

    @Autowired
    public OrderController(OrderService orderService, ItemServiceImpl itemService, AppJwtTokenUtil jwtTokenUtil) {
        this.orderService = orderService;
        this.itemService = itemService;
        this.jwtTokenUtil = jwtTokenUtil;
    }

    @ApiOperation(value = "获取订单", notes = "获取订单")
    @GetMapping("/list")
    public ApiResponse<?> list(HttpServletRequest request, @RequestParam String status) {
        Long userId = jwtTokenUtil.getUserId(request);
        if (userId == null) return ApiResponse.OK(Collections.EMPTY_LIST);
        List<OrderVO> orderVOS = orderService.listByUserAndStatus(userId, status);
        orderVOS.forEach(orderVO -> orderVO.getDetails().forEach(detail -> {
            ItemVO item = itemService.getById(detail.getItemId().intValue());
            detail.setName(item.getName());
            if (item.getMain() != null) {
                detail.setMain(item.getMain().getLink());
            }
            detail.setAuthor(item.getAuthor());
        }));
        return ApiResponse.OK(orderVOS);
    }

    @ApiOperation(value = "订单数量", notes = "订单数量")
    @GetMapping("/count")
    public ApiResponse<Map<String, Integer>> getItemsCount(HttpServletRequest request) {
        Map<String, Integer> map = new HashMap<>();
        Long userId = jwtTokenUtil.getUserId(request);
        if (userId == null) return ApiResponse.OK(map);
        Integer openCnt = orderService.listByUserAndStatus(userId, "ongoing").size();
        Integer closedCnt = orderService.listByUserAndStatus(userId, "closed").size();
        map.put("open", openCnt);
        map.put("closed", closedCnt);
        return ApiResponse.OK(map);
    }

    @ApiOperation(value = "删除订单", notes = "删除订单")
    @GetMapping("/del/{id}")
    public ApiResponse<?> delItem(@PathVariable Integer id) {
        if (id == null) return ApiResponse.OK();
        orderService.removeById(id);
        return ApiResponse.OK();
    }

    @ApiOperation(value = "查询订单状态", notes = "查询订单状态")
    @GetMapping("/checkStatus")
    public ApiResponse<Boolean> delItem(@RequestParam String batchCode) {
        Order order = orderService.getByBatchCode(batchCode);
        if (order == null) return ApiResponse.OK(false);
        return ApiResponse.OK(order.getStatus().equals(EOrderStatus.CLOSED.getStatus()) ||
                order.getStatus().equals(EOrderStatus.FAIL.getStatus()));
    }
}
